var parentId = '';//子集场景id
var firstClassId = ''; //获取一级场景分类
if(getQueryString("parentId")){
     parentId = getQueryString("parentId");

}
// 获取一级场景分类
if(getQueryString("firstClassId")){
    firstClassId = getQueryString("firstClassId");
}
// 接口参数
var params = {
    pageNum:1,
    pageSize:10,
    parentId:parentId,
    sceneId:'',
    subSceneId:'',
    sceneTitle:'',
    startTime:'',
    endTime:''

}
var dataSource = [];
// 获取接口数据
var getData = {
    // 查询列表的渲染
    secondSceneListsInfo:function(){
        var successData = function(data){
            if(data.errorCode == 0){
                if(data.data.result.length >0 && !!data.data){
                    renderData.renderSecondSceneLists(data.data)
                    $(".M-box3").pagination({
                        pageCount: data.data.totalPage,
                        jump: true,
                        coping: true,
                        isHide: false,
                        prevContent: '上页',
                        nextContent: '下页',
                        callback:function(api){
                            params.pageNum = api.getCurrent()
                            var successData = function(data){
                                renderData.renderSecondSceneLists(data.data)
                            }
                            return web.doAjax(root.scenario.queryAppScene, 'post', params, successData);
                        }
                    })
                } else {
                    $(".M-box3").pagination({
                        pageCount:0
                    })
                    renderData.renderSecondSceneLists(data.data)
                }
            }else{
                $.Huimodalalert('数据异常',2000);
            }

        }
        return web.doAjax(root.scenario.queryAppScene, 'post', params, successData);
    },
    // 删除场景
    deleteByIdInfo:function(id){
        var params = {
            id:id
        }
        var successData = function(data){
            if(data.errorCode == 0){
                $.Huimodalalert('删除成功',2000);
                getData.secondSceneListsInfo();
            }
        }
        return web.doAjax(root.scenario.deleteById, 'post', params, successData)
    }
}

// 数据渲染
var renderData = {
    // 子集场景列表渲染
    renderSecondSceneLists:function(data){
        dataSource = [];
        if(!!data && data.result.length > 0){
            $.each(data.result,function(index,item){
                var obj = {};
                obj.createTime = item.createTime;// 操作时间
                obj.updateTime = item.updateTime;
                obj.id = item.id;// 删除数据的唯一标识
                obj.sceneId = item.sceneId;// 一级场景的id
                obj.subSceneId = item.subSceneId;// 二级场景的id
                obj.sceneTitle = item.sceneTitle; // 子集场景名称
                obj.coordinate = item.coordinate; // 位置
                obj.sceneImgUrl = item.sceneImgUrl; // 图片地址
                obj.trItem = item;
                dataSource.push(obj)
            })
        }
        renderData.secondSceneLists(dataSource)
    },
    //表格渲染
    secondSceneLists:function(data){
        var datatable = $("#secondSceneEditList").dataTable(); // 初始化dataTable，清空上一个值
        datatable.fnClearTable();//清空一下table
        datatable.fnDestroy(); //还原初始化了的datatable
        $("#secondSceneEditList").DataTable({
            "order": [[ 1, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据"
            },
            data:data,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var sceneTitle = row.sceneTitle;
                        if(sceneTitle == undefined){
                            sceneTitle = ''
                        }
                        return '<span style="text-align: center;" >'+ sceneTitle +'</span>';
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var coordinate = row.coordinate;
                        if(coordinate == undefined){
                            coordinate = ''
                        }
                        return  '<span style="text-align: center;" >'+ coordinate +'</span>';

                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row, meta) {
                        var operateTime = '';// 操作时间
                        var createTime = row.createTime;
                        var updateTime = row.updateTime;
                        if(!updateTime){
                            operateTime = createTime;
                        }else{
                            operateTime = updateTime;
                        }
                        return  '<span style="text-align: center;">'+ operateTime +'</span>';
                    },
                    "targets": 2
                },
                {
                    "render": function(data, type, row, meta) {
                        return "<span style='text-align: center; color: blue; margin-right: 32px; cursor: pointer' onclick='modify("+JSON.stringify(row.trItem)+")' >修改</span>"
                            +  "<span style='text-align: center; color: #0000ff;cursor: pointer; margin-right: 32px' onclick='cancel("+row.id+")' >删除</span>"
                            +  "<span style='text-align: center; color: #0000ff;cursor: pointer' onclick='management("+row.subSceneId+","+row.id+")' >锚点管理</span>";
                    },
                    "targets": 3
                },
            ],
        })
    }
}
// 逻辑处理
getData.secondSceneListsInfo()
// 查询
function search(){
    params.sceneTitle =  $("#secondSceneName").val();
    getData.secondSceneListsInfo()
}
// 新增 //parent.location.reload();
function add(){
    var addUrl = layer.open({
        type: 2,
        title: "新增",
        content: 'secondSceneManagement.html?operateType='+ 1 + '&parentId='+parentId +'&firstClassId='+firstClassId,
        end:function(){
            location.reload();
        }
    })
    layer.full(addUrl);
}
/* 列表里的事件*/
// 修改
function modify(row){
    // 将修改的数据放入本地存储
    var modifyParams = {
        sceneId:row.sceneId,
        subSceneId:row.subSceneId,
        sceneTitle:row.sceneTitle,
        coordinate:row.coordinate,
        sceneImgUrl:row.sceneImgUrl,
        id:row.id
    }
    sessionStorage.setItem('modifyParams',JSON.stringify(modifyParams))
   var modifyUrl = layer.open({
       type: 2,
       title: "修改",
       content: 'secondSceneManagement.html?operateType='+ 2 +'&id='+ row.id+'&parentId='+parentId + '&firstClassId='+firstClassId,
       end:function(){
           location.reload();
       }
   })
    layer.full(modifyUrl);
}
// 删除
function cancel(id){
    getData.deleteByIdInfo(id)
}
//锚点管理
function management(subSceneId,id){
   // 跳转锚点管理列表页面
    var anchorManagementUrl = layer.open({
        type:2,
        title:'锚点管理',
        content: 'sceneAnchorEditList.html?subSceneId='+subSceneId + "&id="+id
    })
    layer.full(anchorManagementUrl)
}